package com.qst.financing.controller;

import com.qst.financing.common.Result;
import com.qst.financing.common.StatusCode;
import com.qst.financing.mapper.BankUserMapper;
import com.qst.financing.pojo.BankUser;
import com.qst.financing.pojo.DTO.BuserDTO;

import com.qst.financing.properties.JwtProperties;

import com.qst.financing.service.BankUserService;
import com.qst.financing.utils.JwtClaimsConstant;
import com.qst.financing.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.security.auth.login.AccountNotFoundException;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 高歌
 * @Description 用户模块
 * @Date 2021-08-24
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
@Log4j2
@Api(tags = "用户模块接口")
public class BankUserController {
    @Autowired
    private BankUserService bankUserService;

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private BankUserMapper bankUserDao;

    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public Result login(@RequestBody BuserDTO buserDTO) throws AccountNotFoundException {

        BankUser Buser=bankUserService.login(buserDTO);
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, Buser.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        log.info("token:{}", token); // 记录生成的令牌
        return new Result(true,StatusCode.OK,"成功登录",token);
    }


    //用户登录之后，根据用户名展示个人信息
    @ApiOperation(value = "用户登录之后，根据用户名展示个人信息")
    @GetMapping("/loginSelectByUsername")
    public Result<BankUser> loginSelectByUsername() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserDetails principal = (UserDetails) authentication.getPrincipal();
        String username = principal.getUsername();
        BankUser bankUser = bankUserService.selectByUserName(username);
        return new Result<BankUser>(true, StatusCode.OK, "查询成功", bankUser);
    }


    //根据用户名查询用户
    @ApiOperation(value = "根据用户名查询用户")
    @GetMapping("/search/{userName}")
    public Result selectByUserName(@PathVariable("userName") String userName) {
        BankUser bankUser = bankUserService.selectByUserName(userName);
        return new Result(true, StatusCode.OK, "查询成功", bankUser);
    }
}
